package dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.TaiKhoan;

public class TaiKhoanDAO {

	private final Session session = HibernateUtils.getSessionFactory().openSession();
	
	public List<TaiKhoan> listAccount(){
		try {
			session.beginTransaction();
			List<TaiKhoan> result = session.createCriteria(TaiKhoan.class).list();
			session.getTransaction().commit();
			return result;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public TaiKhoan getAccount(String username){
		try {
			session.beginTransaction();
			TaiKhoan tk = (TaiKhoan) session.get(TaiKhoan.class, username);
			session.getTransaction().commit();
			return tk;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean delete(TaiKhoan tk){
		try {
			session.beginTransaction();
			session.delete(tk);
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
	}
	
	public boolean save(TaiKhoan tk){
		try {
			session.beginTransaction();
			session.save(tk);
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
		
	}
	
	public boolean update(TaiKhoan tk){
		try {
			session.beginTransaction();
			session.update(tk);
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
		
	}
	
}
